//
//  SY_Home_LeavingReplyViewController.m
//  ChatDemo-UI2.0
//
//  Created by Sylar on 16/8/16.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "SY_Home_LeavingReplyViewController.h"

@interface SY_Home_LeavingReplyViewController ()<UITextViewDelegate>
{
    UIButton *rightBtn;
    
    UILabel *placeholderLabel;
    UILabel *numberLabel;
}

@property (nonatomic, strong) UITextView *replyTextView;

@end

@implementation SY_Home_LeavingReplyViewController

- (void)loadView {
    [super loadView];
    self.navBar.nav_titleLabel.text=@"请假回复";
    self.view.backgroundColor = RGBCOLOR(240,239,245);
   
    self.navBar.rightButton.hidden = NO;
    [self.navBar.rightButton setTitle:@"发送" forState:UIControlStateNormal];
    [self.navBar.rightButton setTitleColor:RGBACOLOR(221, 221, 221, 1) forState:UIControlStateNormal];
    [self.navBar.rightButton addTarget:self action:@selector(replyBtnActive) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar.rightButton setEnabled:NO];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.replyTextView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark Methods

- (void)replyBtnActive {
    
    if (self.replyTextView.text.length > 150) {
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"回复内容最多为150个字" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:@"api_leave_replyadd" forKey:@"method"];
    [paramDic setObject:USERID forKey:@"userid"];
    [paramDic setObject:self.parentID forKey:@"parentid"];
    [paramDic setObject:self.replyTextView.text forKey:@"scontent"];
    [paramDic setObject:self.toUserID forKey:@"touserid"];
    [paramDic setObject:self.leaveID forKey:@"leaveid"];
    
    [Utils get:nil parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSLog(@"%@",jsonObject);
        if ([[jsonObject objectForKey:@"re_code"] integerValue] == 0) {
            
            if (self.leavingReply) {
                self.leavingReply();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

#pragma mark - TextView Delegate

- (void) textViewDidChange:(UITextView *)textView{
    
    if ([textView.text length] == 0) {
        [placeholderLabel setHidden:NO];
    }else{
        [placeholderLabel setHidden:YES];
    }
    
    NSInteger descriptionNum = textView.text.length;
    
    numberLabel.text = [NSString stringWithFormat:@"%ld/150",(long)descriptionNum];
    numberLabel.textColor = RGBCOLOR(199, 199, 204);
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (self.replyTextView.text.length > 0) {
        [self.navBar.rightButton setEnabled:YES];

        [self.navBar.rightButton setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
    }else {
        [self.navBar.rightButton setEnabled:NO];
        [self.navBar.rightButton setTitleColor:RGBACOLOR(221, 221, 221, 1) forState:UIControlStateNormal];
    }
    
    if ([text isEqualToString:@"\n"]) {
        [self.replyTextView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark - Get

- (UITextView *)replyTextView {
    
    if (_replyTextView == nil) {
        
        _replyTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, 160)];
        [_replyTextView setFont:[UIFont systemFontOfSize:15]];
        [_replyTextView setTextColor:RGBCOLOR(0, 0, 0)];
        _replyTextView.delegate = self;
        _replyTextView.returnKeyType = UIReturnKeyDone;
        
        placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 7, 200, 15)];
        placeholderLabel.enabled = NO;
        placeholderLabel.text = @"请输入回复内容";
        placeholderLabel.font =  [UIFont systemFontOfSize:15];
        placeholderLabel.textColor = RGBCOLOR(221, 221, 221);
        
        [_replyTextView addSubview:placeholderLabel];
        
        numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH - 24 - 220, 145, 200, 10)];
        numberLabel.enabled = NO;
        numberLabel.text = @"0/150";
        numberLabel.textAlignment = NSTextAlignmentRight;
        numberLabel.font =  [UIFont systemFontOfSize:10];
        numberLabel.textColor = RGBCOLOR(199, 199, 204);
        
        [_replyTextView addSubview:numberLabel];
        
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 159.5, SCREENWIDTH, 0.5)];
        line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
        [_replyTextView addSubview:line];
        
    }
    return _replyTextView;
}

@end
